package com.dt.platform.ops.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.dt.platform.ops.service.IMonitorDataProcessPingService;
import com.dt.platform.ops.service.IMonitorNodeService;
import com.dt.platform.proxy.ops.MonitorDataProcessPingServiceProxy;
import com.github.foxnic.api.transter.Result;
import com.github.foxnic.dao.data.Rcd;
import com.github.foxnic.dao.data.RcdSet;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.github.foxnic.web.framework.sentinel.SentinelExceptionUtil;
import org.github.foxnic.web.framework.web.SuperController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "Ping取数")
@ApiSort(0)
@RestController("MonitorDataProcessPingController")
public class MonitorDataProcessPingController extends SuperController {

    @Autowired
    IMonitorDataProcessPingService monitorDataProcessPingService;


    @Autowired
    private IMonitorNodeService monitorNodeService;


    /**
     * 批量删除节点 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "收集数据")
    @ApiOperationSupport(order=1)
    @SentinelResource(value = MonitorDataProcessPingServiceProxy.COLLECT_DATA , blockHandlerClass = { SentinelExceptionUtil.class } , blockHandler = SentinelExceptionUtil.HANDLER )
    @PostMapping(MonitorDataProcessPingServiceProxy.COLLECT_DATA)
    public Result collectData() {
        return monitorDataProcessPingService.collectData();
    }

    /**
     * 批量删除节点 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "收集数据")
    @ApiOperationSupport(order=1)
    @SentinelResource(value = MonitorDataProcessPingServiceProxy.QUERY_DATA , blockHandlerClass = { SentinelExceptionUtil.class } , blockHandler = SentinelExceptionUtil.HANDLER )
    @GetMapping(MonitorDataProcessPingServiceProxy.QUERY_DATA)
    public Result queryData(String categoryIds,String code,String minute) {
        //code:pingV4
        //ping:pingV6
        Result <JSONObject> res=new Result<>();
        JSONObject resObj=new JSONObject();

        String sql="\n" +
                "select node.id,node.node_ip,node.node_ip_v6,node.node_name_show,\n" +
                "list_value_str1 time," +
                "value_int1 seq," +
                "value_number1 delay," +
                "value_str1 status," +
                "value_str2 reason," +
                "node_data.record_time\n" +
                " from (\n" +
                "select * from ops_monitor_node n where n.node_enabled='enable' and deleted=0 \n" +
                "and n.id in (\n" +
                "select rule.book_id from ops_monitor_alert_book_rule rule where rule.deleted=0 and rule.type='node_group' and rule.value=?\n" +
                ")\n" +
                "and n.id in (\n" +
                "select distinct b.node_id from ops_monitor_tpl_indicator a,ops_monitor_node_tpl_item b\n" +
                "where a.code=? and a.monitor_method='ping'\n" +
                "and a.deleted=0\n" +
                "and a.monitor_tpl_code=b.tpl_code\n" +
                ")\n" +
                ")node left join (select * from ops_monitor_node_value_last \n" +
                "where (node_id,record_time) in (\n" +
                "select node_id,max(record_time) max_record_time from ops_monitor_node_value_last \n" +
                "where indicator_code=? \n" +
                "and record_time>NOW() - INTERVAL 60*5 SECOND\n" +
                "group by node_id )) node_data on node.id=node_data.node_id \n";
        System.out.println("sql:"+sql);
        String[] categoryIdsArr=categoryIds.split(",");
        for(int i=0;i<categoryIdsArr.length;i++){
            String categoryId=categoryIdsArr[i];
            Rcd rcd=monitorNodeService.dao().queryRecord("select * from ops_monitor_node_group where id=?",categoryId);
            JSONObject itemObj=new JSONObject();
            if(rcd!=null){
                itemObj=rcd.toJSONObject();
                RcdSet rs=monitorNodeService.dao().query(sql,categoryId,code,code);
                itemObj.put("collectData",rs.toJSONArrayWithJSONObject());
            }
            resObj.put(categoryId,itemObj);
        }
        res.data(resObj);
        res.success(true);
        return res;

    }

}
